//
//  ScanCodeViewController.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/8.
//

#import "ScanCodeViewController.h"
#import <SGQRCode.h>
#import "ImagePickerManager.h"
#import "ShopInfoViewController.h"
#import "ShopViewController.h"

@interface ScanCodeViewController ()<SGScanCodeDelegate>

@property (nonatomic, strong) ImagePickerManager *pickManager;
@property (nonatomic, strong) SGScanCode *scanCode;
@property (nonatomic, strong) SGScanView *scanView;
@property (nonatomic, strong) UILabel *tipLabel;

@end

@implementation ScanCodeViewController

- (void)initDidLoad {
    self.hbd_barAlpha = 0;
    self.navigationTitle = @"扫一扫";
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [btn setTitle:@"相册" forState:UIControlStateNormal];
    [btn setTitleColor:kBgColor forState:UIControlStateNormal];
    btn.titleLabel.font = kFontMediium(16);
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = right;
    @weakify(self)
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        @strongify(self)
        [self showImagePicker];
    }];
}

- (void)initViews {
    [self.view addSubview:self.scanView];
    [self.view addSubview:self.tipLabel];
    
    self.scanCode = [SGScanCode scanCode];
    self.scanCode.preview = self.view;
    self.scanCode.delegate = self;
    [self.scanCode startRunning];
}

#pragma mark - action
- (void)showImagePicker {
    [self.scanCode stopRunning];
    [self.scanView stopScanning];
    
    [self.pickManager showNoEditPickInVC:self sourceType:UIImagePickerControllerSourceTypePhotoLibrary completion:^(UIImage * _Nullable img) {
        if (img) {
            [self.scanCode readQRCode:img completion:^(NSString *result) {
                if (!result) {
                    [self.scanCode startRunning];
                    [self.scanView startScanning];
                    kShowText(@"未识别出二维码");
                }else {
                    [self getScanResult:result];
                }
            }];
        }else {
            [self.scanCode startRunning];
            [self.scanView startScanning];
        }
    }];
}
     
- (void)getScanResult:(NSString *)result {
    NSLog(@"%@",result);
    if (result.length <= 0) {
        [self.scanCode startRunning];
        [self.scanView startScanning];
        kShowText(@"未识别出二维码");
        return;
    }
    if (![result containsString:@"bomain"] && ![result containsString:@"compon"]) {
        /// 打开web页面
        return;
    }
    [self.scanCode stopRunning];
    [self.scanView stopScanning];
    NSRange range = [result rangeOfString:@"id=" options:NSBackwardsSearch];
    NSString *str = [result substringFromIndex:range.location+range.length];
    NSRange range1 = [str rangeOfString:@"&"];
    NSString *str1 = [str substringToIndex:range1.location];
    if ([result containsString:@"bomain"]) {
        /// 个人
        ShopViewController *vc = [[ShopViewController alloc] init];
        vc.userId = str1;
        vc.isPush = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        /// 商品
        ShopInfoViewController *vc = [[ShopInfoViewController alloc] init];
        vc.goodsId = str1;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - 懒加载
- (SGScanView *)scanView {
    if (!_scanView) {
        SGScanViewConfigure *config = [[SGScanViewConfigure alloc] init];
        config.isShowBorder = YES;
        config.borderColor = [UIColor clearColor];
        config.cornerColor = kBgColor;
        config.cornerWidth = 2;
        config.cornerLength = 15;
        config.isFromTop = YES;
        config.scanline = @"scan_line";
        config.color = [UIColor clearColor];
        
        _scanView = [[SGScanView alloc] initWithFrame:self.view.bounds configure:config];
        [_scanView startScanning];
        _scanView.scanFrame = self.view.bounds;
    }
    return _scanView;
}

- (UILabel *)tipLabel {
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0.73*kHeightForScreen, kWidthForScreen, 25)];
        _tipLabel.textAlignment = NSTextAlignmentCenter;
        _tipLabel.font = kFontBold(13);
        _tipLabel.textColor = kRGB_ALPHA(0xFFFFFF, 0.7);
        _tipLabel.text = @"将二维码放入框内,即可自动扫描";
    }
    return _tipLabel;
}

- (ImagePickerManager *)pickManager {
    if (!_pickManager) {
        _pickManager = [[ImagePickerManager alloc] init];
    }
    return _pickManager;
}

#pragma mark - delegate
- (void)scanCode:(SGScanCode *)scanCode result:(NSString *)result {
    [self getScanResult:result];
}

- (void)dealloc {
    [self.scanCode stopRunning];
    [self.scanView stopScanning];
}
@end
